﻿using Admin.Service.Test;
using Autofac;
using Autofac.Extensions.DependencyInjection;
using Castle.Core.Smtp;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Linq;
using System.Xml.Linq;

namespace Admin.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class TestController : ControllerBase
    {
        private readonly IEmailService email;

        private readonly IServiceProvider service;

        public TestController(IEmailService email, IServiceProvider service)
        {
            this.email = email;
            this.service = service;
            //this.serviceProvider = serviceProvider;
            //var obj1 = serviceProvider.GetServices(typeof(IEmailService));
            ////var obj2 = serviceProvider.GetService(typeof(MyEmailService));
            //var email = obj1.FirstOrDefault(x => x.GetType() == typeof(EmailService));
            //var myemail = obj1.FirstOrDefault(x => x.GetType() == typeof(MyEmailService));       
        }

        [HttpGet]
        public IActionResult Test([FromServices] ILifetimeScope lifetime)
        {
            var one = lifetime.ResolveNamed<IEmailService>("one");

            //其中service为在构造函数中注入的IServiceProvider接口
            var two = service.GetAutofacRoot().ResolveNamed<IEmailService>("two");

            return Ok();
        }
    }
}
